package com.fun_ordering.service.impl;

import com.fun_ordering.service.DishRatioService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.DishRatioMapper;
import com.fun_ordering.pojo.DishRatio;
import com.fun_ordering.pojo.DishRatioExample;
import com.fun_ordering.pojo.DishRatioExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现层
 * @author XiaoPi
 *
 */
@Service
public class DishRatioServiceImpl implements DishRatioService {

	@Autowired
	private DishRatioMapper dishRatioMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<DishRatio> findAll() {
//		System.out.println(dishRatioMapper.selectByExample(null));
		return dishRatioMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<DishRatio> page=   (Page<DishRatio>) dishRatioMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(DishRatio dishRatio) {
		dishRatioMapper.insert(dishRatio);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(DishRatio dishRatio){
		dishRatioMapper.updateByPrimaryKey(dishRatio);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param ratioId
	 * @return
	 */
	@Override
	public DishRatio findOne(Integer ratioId){
		return dishRatioMapper.selectByPrimaryKey(ratioId);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] ratioIds) {
		for(Integer ratioId:ratioIds){
			dishRatioMapper.deleteByPrimaryKey(ratioId);
		}		
	}

	@Override
	public void deleteByDishId(Integer dishId) {
		DishRatioExample example = new DishRatioExample();
		Criteria criteria = example.createCriteria();
		criteria.andDishIdEqualTo(dishId);
		dishRatioMapper.deleteByExample(example);
	}


	@Override
	public PageResult findPage(DishRatio dishRatio, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		DishRatioExample example=new DishRatioExample();
		Criteria criteria = example.createCriteria();
		
		if(dishRatio!=null){			
				
		}
		
		Page<DishRatio> page= (Page<DishRatio>)dishRatioMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
